package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.Check;
import com.quwan.common.exception.ExpCode;
import com.quwan.common.result.ApiResult;
import com.quwan.entity.TMenu;
import com.quwan.manager.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author laozhang
 * @date 2021/9/25 11:47
 */
@Api(tags = "菜单")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/menu")
public class MenuController extends BaseController {

    private final MenuService menuService;

    @ApiOperation(value = "菜单列表", notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;roleName——角色名称;remarks——备注;addBy——增加人;addTime——增加时间;</span>", httpMethod = "POST")
    @PostMapping("list")
    public ApiResult<List<TMenu>> menuPage(@ApiParam(value = "查询条件") String menuName) {
        List<TMenu> res = menuService.getMenuListPage(menuName);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "一级菜单列表", notes = "<span style=\"color:red\">注意参数是否必填,返回数据中id——主键;roleName——角色名称;remarks——备注;addBy——增加人;addTime——增加时间;</span>", httpMethod = "POST")
    @PostMapping("parent")
    public ApiResult<List<TMenu>> parentMenuList() {
        List<TMenu> res = menuService.getParentMenuList();
        return ApiResult.success(res);
    }

    @ApiOperation("菜单修改")
    @PostMapping("updateMenu")
    public ApiResult updateMenu(TMenu menu) {

        boolean success = menuService.updateMenu(menu);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addMenu")
    @ApiOperation("添加菜单")
    public ApiResult addMenu(HttpServletRequest request, TMenu menu) {
        Check.notNull(menu, ExpCode.PARAMS_ILLEGAL);
        TokenInfo tokenInfo = getTokenInfo(request);
        menu.setAddBy(tokenInfo.getId());
        menu.setAddTime(new Date());
        boolean success = menuService.addMenu(menu);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }


    @PostMapping("delMenu")
    @ApiOperation("删除菜单")
    public ApiResult delRole(@ApiParam(value = "id主键") @RequestParam(value = "id") Integer id) {

        boolean success = menuService.delMenu(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }
    @PostMapping("status")
    @ApiOperation("修改菜单状态")
    public ApiResult status(@ApiParam(value = "id主键") @RequestParam(value = "id") Integer id,
                            @ApiParam(value = "0禁用 1开启") @RequestParam(value = "status") Integer status) {

        boolean success = menuService.status(id,status);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

}
